/**
 * 气泡漂浮效果
 * @author  陆楚良 <lu_chuliang@sina.com>
 * @version 1.0
 * https://git.oschina.net/luchg/jquery.flotage.js
 * 依赖：
 *     需要jQuery和Animate<https://git.oschina.net/luchg/CL_Animate.js>的支持
 */
!function (window, factory) {
    if (typeof exports === 'object'){
        module.exports = factory();
    } else if (typeof define === 'function') {
        define(factory);
    } else {
        factory()(jQuery);
    }
}(this, function () {
    return function($){
        $.fn.flotage = $.fn.flotage || function(option){
            var default_option = $.extend({
                min_x: -20,
                min_y: -20,
                max_x: +20,
                max_y: +20
            }, option || {});
            var random = function(Min, Max) {
                var Range = Max - Min;
                var Rand = Math.random();
                return (Min + Math.round(Rand * Range));
            };
            this.each(function(){
                var $this = $(this)
                  , option= $.extend({}, default_option)
                  , A     = new Animate()
                  , pos   = {x:0, y:0}
                ;
                var animate = function(){
                    var x  = random(0, 10)
                      , y  = random(0, 10)
                      , ox = pos.x
                      , oy = pos.y
                    ;
                    x = ox + (Math.floor(Math.random()*10) < 5 ? -x : x);
                    y = oy + (Math.floor(Math.random()*10) < 5 ? -y : y);
                    x = x < option.min_x ? option.min_x : (x > option.max_x ? option.max_x : x);
                    y = y < option.min_y ? option.min_y : (y > option.max_y ? option.max_y : y);
                    x -= ox;
                    y -= oy;
                    A.stop().init(2000 + random(0, 500)).run(function(p){
                        pos.x = ox + x * p;
                        pos.y = oy + y * p;
                        $this.css("transform", "translate("+pos.x+"px,"+pos.y+"px)");
                    },function(){ animate() });
                };
                animate();
            });
        };
    };
});
